package com.dji.activationDemo.uitls;

import android.os.Environment;

import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.text.SimpleDateFormat;

public class WriteLog {

    private static SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
    private static long timeLong = System.currentTimeMillis();
    private static String fileName = format.format(timeLong);


    /**
     * 追加文件：使用FileWriter
     *
     * @param content
     */
    public static void writeLog(String content) {
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        long nowTimeLong = System.currentTimeMillis();
        String nowTime = simpleDateFormat.format(nowTimeLong);
        String path = Environment.getExternalStorageDirectory().getAbsolutePath() + "/Log/"; //文件路径
        FileWriter writer = null;
        try {
            File file = new File(path);
            if (!file.exists()) {  //没有创建文件夹则创建
                file.mkdirs();
            }
            // 打开一个写文件器，构造函数中的第二个参数true表示以追加形式写文件
            writer = new FileWriter(path + fileName + ".txt", true);
            writer.write(nowTime + ":" + content + "\n");
            writer.flush();
            if (writer != null) {
                //关闭流
                writer.close();
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
